﻿using Adventure.Model;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Adventure
{
    internal class HUD
    {
        #region Constants

        private const string GAME_OVER_TEXT = "GAME OVER";

        #endregion

        #region Constructors

        internal HUD(Rectangle bounds, Universe universe)
        {
            _bounds = bounds;
            _universe = universe;
        }

        #endregion

        #region Private Fields

        private Rectangle _bounds;
        private SpriteFont _font;
        private string _fpsText = string.Empty;
        private Vector2 _fpsPosition = Vector2.Zero;
        private Vector2 _gameOverPosition = Vector2.Zero;
        private TimeSpan _miliseconds = TimeSpan.Zero;
        private int _frames = 0;
        private int _lastFrames = 0;
        private Universe _universe;

        #endregion

        #region Internal Methods

        internal void Initialize()
        {
            _font = GraphicsResources.ContentManager.Load<SpriteFont>(@"SpriteFonts\OrbitronSmall");

            var theEndSize = _font.MeasureString(GAME_OVER_TEXT);
            _gameOverPosition = new Vector2(_bounds.Center.X - theEndSize.X / 2f, _bounds.Center.Y - theEndSize.Y / 2f);
        }

        internal void Update(GameTime gameTime)
        {
            _miliseconds += gameTime.ElapsedGameTime;

            if (_miliseconds > TimeSpan.FromSeconds(1))
            {
                _miliseconds -= TimeSpan.FromSeconds(1);
                _lastFrames = _frames;
                _fpsText = _lastFrames.ToString();
                var textSize = _font.MeasureString(_fpsText);
                _fpsPosition = new Vector2(_bounds.Width - textSize.X - 5, 0);

                _frames = 0;
            }
        }

        internal void Draw(GameTime gameTime)
        {
            _frames++;

            GraphicsResources.SpriteBatch.Begin();

            GraphicsResources.SpriteBatch.DrawString(_font, _fpsText, _fpsPosition, Color.Red);

            if (_universe.GameEnded)
                GraphicsResources.SpriteBatch.DrawString(_font, GAME_OVER_TEXT, _gameOverPosition, Color.Red);

            GraphicsResources.SpriteBatch.End();
        }

        #endregion
    }
}
